<@override name="page-title">
新闻动态管理
</@override>
<@override name="page-style">
<link href="/assets/admin/css/plugins/bootstrap-table/bootstrap-table.min.css?v=1.0" rel="stylesheet">
</@override>


<@override name="page-body">
<div class="row wrapper border-bottom white-bg page-heading">
    <div class="col-sm-4">
        <h2>新闻动态管理</h2>
        <ol class="breadcrumb">
            <li>
                <span>主页<span>
            </li>
            <li>
                <strong>新闻动态管理</strong>
            </li>
        </ol>
    </div>
    <div class="col-sm-8">
        <div class="title-action">
            <a href="javascript:location.replace(location.href);" class="btn btn-default"><i class='fa fa-refresh'></i>
                刷新</a>
        </div>
    </div>
</div>
<div class="row">
    <div class="col-sm-12">
        <div class="ibox">
            <div class="ibox-content">
                <form role="form" class="form-inline" id="searchForm">
                    <div class="form-group">
                        <select class="form-control" name="type">
                            <option value="0">选择新闻类型</option>
                            <#list typeList as type>
                                <option value="${type.code}">${type.desc}</option>
                            </#list>
                        </select>
                    </div>
                    <div class="form-group">
                        <input type="text" placeholder="请输入新闻标题" name="title" class="form-control">
                    </div>
                    <div class="form-group">
                        <button type="button" class="btn btn-primary" id="searchBtn">搜索</button>
                    </div>
                </form>
                <div class="btn-group" id="toolbar" role="group">
                    <button type="button" class="btn btn-outline btn-primary" id="addBtn">
                        <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>新增
                    </button>
                </div>
                <table id="dataTableId"></table>
            </div>
        </div>
    </div>
</div>
</@override>

<@override name="page-script">
<script src="/assets/admin/js/plugins/bootstrap-table/bootstrap-table.min.js"></script>
<script src="/assets/admin/js/plugins/bootstrap-table/mobile/bootstrap-table-mobile.min.js"></script>
<script src="/assets/admin/js/plugins/bootstrap-table/locale/bootstrap-table-zh-CN.min.js"></script>
<script src="/assets/admin/js/plugins/layer-v3.0/layer.js"></script>
<script>
    var $table;
    $(function () {
        //1.初始化Table
        $table = $('#dataTableId').bootstrapTable({
            url: '/admin/article/list.json',       //请求后台的URL（*）
            method: 'get',                      //请求方式（*）
            toolbar: '#toolbar',                //工具按钮用哪个容器
            striped: true,                      //是否显示行间隔色
            cache: false,                       //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
            pagination: true,                   //是否显示分页（*）
            sortable: false,                     //是否启用排序
            queryParams: function (d) {         //传递参数（*）
                return $("#searchForm").serialize() + "&majorType=NEWS&limit=" + d.limit + "&offset=" + d.offset;
            },
            sidePagination: "server",           //分页方式：client客户端分页，server服务端分页（*）
            pageNumber: 1,                       //初始化加载第一页，默认第一页
            pageSize: 10,                       //每页的记录行数（*）
            pageList: [10, 20, 30, 50],        //可供选择的每页的行数（*）
            columns: [{
                field: 'id',
                title: 'ID'
            }, {
                field: 'source',
                title: '来源'
            }, {
                field: 'author',
                title: '作者'
            }, {
                field: 'title',
                title: '标题'
            }, {
                field: 'minorType',
                title: '类型',
                formatter: function (value) {
                    if (value === 'FRONT') {
                        return "前沿要闻";
                    }
                    if (value === 'LOCAL') {
                        return "地方新声";
                    }
                    if (value === 'COLLEGE') {
                        return "高校动态";
                    }
                    if (value === 'EXTRATERRITORIAL') {
                        return "域外传真";
                    }
                    return value;
                }
            }, {
                field: 'summary',
                title: '简介'
            }, {
                field: 'readCount',
                title: '浏览次数'
            }, {
                field: 'displayOrder',
                title: '展示序号'
            }, {
                field: 'id',
                title: '操作',
                formatter: function (dataId, row, index) {
                    var html = "<button class='btn btn-info btn-sm' type='button' onclick='data_edit(" + dataId + ")'><i class='fa fa-edit'></i> <span class='bold'>修改</span></button>"
                    html = html + "&nbsp;&nbsp;<button class='btn btn-danger btn-sm' type='button' onclick='data_del(" + dataId + ")'><i class='fa fa-arrow-circle-o-down'></i> <span class='bold'>删除</span></button>";
                    return html;
                }
            }]
        });

        $("#searchBtn").click(function () {
            $table.bootstrapTable('refresh');
        });

        $("#addBtn").click(function () {
            parent.layer.open({
                type: 2,
                title: "添加新闻动态",
                content: "/admin/article/add.htm?majorType=NEWS",
                area: ['600px', '600px'],
                maxmin: true,
                end: function () {
                    var layerData = parent.$("#layer-data").val();
                    if ($.trim(layerData).length == 0) {
                        return;
                    }
                    parent.$("#layer-data").val("");
                    if (layerData == '1') {
                        layer.msg('添加成功！', {
                            icon: 1
                        });
                        $table.bootstrapTable('refresh');
                    } else {
                        layer.msg('添加失败！失败原因:' + layerData, {
                            icon: 2
                        });
                    }
                }
            });
        });

    });
    /*资讯-编辑*/
    function data_edit(dataId) {
        parent.layer.open({
            type: 2,
            title: "修改新闻动态",
            content: "/admin/article/edit.htm?articleId=" + dataId,
            area: ['600px', '600px'],
            end: function () {
                var layerData = parent.$("#layer-data").val();
                if ($.trim(layerData).length == 0) {
                    return;
                }
                parent.$("#layer-data").val("");
                if (layerData == '1') {
                    $table.bootstrapTable('refresh');
                    layer.msg('修改成功！', {
                        icon: 1
                    });
                } else {
                    layer.msg('修改失败！失败原因:' + layerData, {
                        icon: 2
                    });
                }
            }
        });
    }

    function data_del(dataId) {
        parent.layer.confirm('您确定要删除该新闻动态吗？', function () {
            $.ajax({
                type: 'POST',
                url: '/admin/article/delete.json?articleId=' + dataId,
                dataType: 'json',
                success: function (data) {
                    if (data.ret) {
                        $table.bootstrapTable('refresh');
                        parent.layer.msg('删除成功', {shift: 6});
                    } else {
                        parent.layer.msg('删除失败,失败原因:' + data.errmsg, {shift: 6});
                    }
                },
                error: function (data) {
                    console.log(data.errmsg);
                },
            });
        });
    }
</script>

</@override>
<@extends name="admin/common/common.ftl"/>